package svgedit.gui.actions;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.AbstractAction;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

import svgedit.gui.Frame;
import svgedit.gui.PNGFileFilter;
import svgedit.gui.View;
import svgedit.svg.SVGDocument;
import svgedit.svg.SVGElement;

/** Shows a file dialog for the user to export file as png
 */
@SuppressWarnings("serial")
public class ExportAsPNGAction extends AbstractAction {

    private Frame frame;
    private SVGDocument document;
    private View view;
    private int canvasWidth;
    private int canvasHeight;
    /** Create this action for the given frame */
    public ExportAsPNGAction(Frame frame) {
        super("Export As PNG");
        this.frame = frame;
    }

    /**
     *   When invoke this action to save file as another format
     *   
     *   @param arg0 to monitor the action event
     * */
    public void actionPerformed(ActionEvent arg0) {
        File file = frame.getDocument().getFile();
        JFileChooser dialog;
        if (file != null)
            dialog = new JFileChooser(file);
        else
            dialog = new JFileChooser(frame.getPreferences().getDefaultPath());
        // Add the file filter
        dialog.addChoosableFileFilter(new PNGFileFilter());
        dialog.showSaveDialog(null);

        file = dialog.getSelectedFile();
        if (file != null) {
            if (file.exists()) {
                if (JOptionPane.showConfirmDialog(frame.getRootPane(), String.format("Do you want to replace the existing file named %s?", file.getName()), "Overwrite existing file?", JOptionPane.OK_CANCEL_OPTION) == JOptionPane.CANCEL_OPTION)
                    return;
            }
            frame.getPreferences().setDefaultPath(file.getPath());
            try {
				SaveAsPNG(file);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
        }
    }
     
    /**
     *   Save file as png file, 
     *      call function {@link svgedit.gui.View#paintElement(Graphics2D, SVGElement)}
     *   
     *   @param file to get file object 
     * */
    public void SaveAsPNG(File file) throws IOException{
    	document = frame.getDocument();
    	view = frame.getView();
    	canvasHeight = (int)document.getHeight().getValue();
    	canvasWidth = (int)document.getWidth().getValue();
    	BufferedImage buffer = new BufferedImage(canvasWidth, canvasHeight, BufferedImage.TYPE_INT_ARGB);
    	Graphics g = buffer.createGraphics();
    	// Initial canvas
    	g.setColor(Color.WHITE);
        g.fillRect(0, 0, canvasWidth, canvasHeight);
        System.out.println(document.getRootGroup().getChildren().length);
       for (SVGElement elem : document) {
        	view.paintElement((Graphics2D)g, elem);
        }
       String newfile = file.toString();
       if(!file.toString().endsWith(".png")){
   		  newfile = file.toString()+".png";
   	   }
       // Write to the image
    	ImageIO.write(buffer, "PNG", new File(newfile));
    }
    
   

}
